extends Node2D

@export var row = 8
@export var col = 7
@export var row_len = 130
@export var col_len = 130
@export var edge_range = 5 # odd
@export var edge_p = 0.88
@export var bias = Vector2(-350,-250)
@export var random_bias = 10 # even
@export var map = []
@export var point_position = []
@export var point_down = []
@export var point_up = []
@export var point_tot = 0
@export var current_node = 0
@onready var point = preload("res://Scenes/Point.tscn")

func _ready() -> void:
	randomize()
	
	for i in row*col :
		map.append(0)
	for i in row*col :
		point_down.append([])
		point_up.append([])
	
	map[floor(1.0*col/2)] = true
	for i in row-1 :
		var lst = -1
		for j in col :
			if not map[i*col+j] : continue
			var out_cnt = randi_range(2,edge_range-1)
			if i==0 : out_cnt = max(out_cnt,3)
			var out = []
			for k in edge_range : out.append(0)
			for k in out_cnt : out[k] = 1
			out.shuffle()
			for k in edge_range :
				var new_pos = j+k-floor(1.0*(edge_range-1)/2)
				if new_pos<0 or new_pos>=col or new_pos<lst : continue
				if not out[k] : continue
				if i!=0 and randf()>edge_p : continue
				point_down[i*col+j].append((i+1)*col+(new_pos))
				map[(i+1)*col+new_pos] = true
				lst = new_pos
				print("!",i+1," ",new_pos)
	
	var updated = false
	while not updated :
		updated = true
		for i in row-1 :
			for j in col :
				if map[i*col+j] and point_down[i*col+j].size()==0 :
					map[i*col+j] = false
					updated = false
					
					for k in row :
						for p in col :
							for t in point_down[k*col+p].size() :
								if point_down[k*col+p][t]==i*col+j :
									point_down[k*col+p].remove_at(t)
									break
	
	for i in row*col :
		if not map[i] : continue
		for k in point_down[i] : point_up[k].append(i)
	
	for i in row :
		for j in col :
			point_position.append(bias + Vector2(col_len*j+randi_range(-random_bias,random_bias),row_len*i+randi_range(-random_bias,random_bias)))
			
			if not map[i*col+j] : continue
			
			point_tot += 1
			map[i*col+j] = point_tot
			var new_point = point.instantiate()
			new_point.position = point_position[i*col+j]
			new_point.name = "Point"+str(point_tot)
			new_point.id = point_tot
			new_point.rowcol_id = i*col+j
			add_child(new_point)
			print(i," ",j)		
	
	queue_redraw()

func _draw() -> void:
	for i in row*col :
		if not map[i] : continue 
		get_child(map[i]-1).avaliable = false
			
	if current_node==0 : 
		for i in col :
			if map[(row-1)*col+i] :
				get_child(map[(row-1)*col+i]-1).avaliable = true
	else :
		var current_rowcol_id = get_child(current_node-1).rowcol_id
		for i in point_up[current_rowcol_id] :
			get_child(map[i]-1).avaliable = true 

	for i in row*col : 
		for j in point_down[i] :
			var snode = get_child(map[i]-1)
			var enode = get_child(map[j]-1)
			if snode.visited and enode.visited : draw_line(point_position[i],point_position[j],Color(1,1,0),5.0,true)
			else : draw_dashed_line(point_position[i],point_position[j],Color(0.65,0.5,0.2),4.0,9.0,false,true)

func _process(delta: float) -> void:
	pass
